<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class m_transaksi_pembelian extends CI_Model {
	var $table = "beli";

	public function get_all(){
		$sql = "SELECT * FROM $this->table b 
		JOIN rc_users u ON b.id_user = u.id
		ORDER BY KdBeli DESC
		";
		return $sql;
	}

	public function get_obat(){
		$query = $this->db->get('obat')->result_array();
		return $query;
	}

	public function search($filter, $keyword){
		if($filter == "by_waktu"){
			$sql = "SELECT * FROM $this->table b 
			JOIN rc_users u ON b.id_user = u.id
			WHERE Tanggal BETWEEN '$keyword[0]' AND '$keyword[1]'
			ORDER BY KdBeli DESC
			";			
		}else if($filter == "by_petugas"){
			$sql = "SELECT * FROM $this->table b 
			JOIN rc_users u ON b.id_user = u.id
			WHERE id_user = $keyword
			ORDER BY KdBeli DESC
			";
		}else if($filter == "by_kelompok"){
			$sql = "SELECT * FROM $this->table b 
			JOIN rc_users u ON b.id_user = u.id
			JOIN detail_beli db ON b.KdBeli = db.KdBeli
			JOIN obat o ON db.KdObat = o.KdObat
			WHERE KdKelompok = $keyword
			ORDER BY b.KdBeli DESC
			";
		}else if($filter == "by_obat"){
			$sql = "SELECT * FROM $this->table b 
			JOIN rc_users u ON b.id_user = u.id
			JOIN detail_beli db ON b.KdBeli = db.KdBeli
			JOIN obat o ON db.KdObat = o.KdObat
			WHERE db.KdObat = $keyword
			ORDER BY b.KdBeli DESC
			";
		}else{
			$sql = array();
		}
		return $sql;
	}

	public function store($input){
		$data1 = array(
			"Tanggal"	=> $input['tanggal'],
			"Keterangan" => $input['keterangan'],
			"id_user"	=> $input['user']
			);
		$query1 = $this->db->insert($this->table, $data1);
		if($query1){
			$this->db->select_max('KdBeli');
			$max_id = $this->db->get('beli')->result_array();
			for($i=0; $i<count($input['obat']); $i++){
				$data2 = array(
					"KdBeli"	=> $max_id[0]['KdBeli'],
					"KdObat" => $input['obat'][$i],
					"Jumlah"	=> $input['jumlah'][$i],
					"Harga"	=> $input['harga'][$i]
					);
				$query2 = $this->db->insert("detail_beli", $data2);
			}
			if($query2){
				set_flash('success','Data detail pembelian berhasil ditambahkan');
				return false;
			}else{
				set_flash('danger','Data detail pembelian gagal ditambahkan');
				return false;	
			}
		}else{
			set_flash('danger','Data pembelian gagal ditambahkan');
			return false;
		}
	}

	public function get_detail_byid($id){
		$this->db->where('KdBeli', $id);
		$query = $this->db->get('detail_beli')->result_array();
		return $query;
	}

	public function get_by_id($id){
		$this->db->where('KdBeli', $id);
		$this->db->join('rc_users', 'beli.id_user = rc_users.id');
		$query = $this->db->get($this->table);

		if($query->num_rows() > 0){
			$result = $query->row_array();
			$query->free_result();
		}else{
			$result = array();
		}

		return $result;
	}

	public function update($param, $id){
		$this->db->where('KdBeli', $id);
		$del_ex_recs = $this->db->delete('detail_beli');

		if($del_ex_recs){
			#update beli
			$data_beli = array(
				'Keterangan'	=> $param['keterangan']
				);
			$this->db->where('KdBeli', $id);
			$do_update = $this->db->update($this->table, $data_beli);
			
			if($do_update){
				if(isset($param['obat'])){
					for($i=0; $i<count($param['obat']); $i++){
						$data_detail_beli = array(
							'KdBeli'	=> $id,
							'KdObat'	=> $param['obat'][$i],
							'Jumlah'	=> $param['jumlah'][$i],
							'Harga'	=> $param['harga'][$i]
							);
						$ins_detail = $this->db->insert('detail_beli', $data_detail_beli);
					}
					if($ins_detail){
						set_flash('success','Data pembelian berhasil diperbaharui');
						return false;			
					}else{
						set_flash('danger','Data pembelian gagal diperbaharui');
						return false;			
					}
				}else{
					set_flash('success','Data pembelian berhasil diperbaharui');
					return false;			
				}
			}else{
				set_flash('danger','Data pembelian gagal diperbaharui');
				return false;	
			}
		}else{
			set_flash('danger','Data pembelian gagal diperbaharui');
			return false;
		}
	}

	public function destroy($id){
		$this->db->where('KdBeli', $id);
		$destroy_child = $this->db->delete('detail_beli');

		if($destroy_child){
			$this->db->where('KdBeli', $id);
			$do_destroy = $this->db->delete($this->table);			
			if($do_destroy){
				set_flash('success','Data pembelian berhasil dihapus');
				return false;		
			}else{
				set_flash('danger','Data pembelian gagal dihapus');
				return false;	
			}
		}else{
			set_flash('danger','Data pembelian gagal dihapus');
			return false;	
		}
	}

	public function export(){
		$this->db->join('rc_users', 'beli.id_user = rc_users.id');
		$query = $this->db->get($this->table);
		if ($query->num_rows() > 0) {
			$result = $query->result_array();
			$query->free_result();
		} else {
			$result = array();
		}
		return $result;
	}
}

/* End of file m_transaksi_pembelian.php */
/* Location: ./application/models/m_transaksi_pembelian.php */








